package main;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;

import database.Database;

import Gui.NewJFrame;
import Gui.login.Login;
import main.userdb.UserDatabase;

public class Main {
	
	public final static boolean admin = false; // 2 versies: 1 true (admin) en 1 false (studenten)

    public static boolean save = true;
	
	public static Connection connection;
	public static Database db = new Database("test.db3");
	
    public final static String userDBName = "usr.db3";
    public static Connection userDBConnection;
    public final static UserDatabase userDB = new UserDatabase();

    
	public static void main(String[] args) {
		
		/* 
		 * Show login panel, get username and password from the user, get the dbName that 
		 * relates to these strings and perform the next actions: open GUI + dbManipulation
		 */
		
/** to try, use as login: test and as password: test */
		
		initUserDB();
        
		if (userDB.isEmpty()) {
			// TODO open newUserPanel + open Login -> in newUserPanel
		} else {
			Login.main(null);
		}

        if (!save) {
            File f = new File(db.getDBName());
            f.delete();
            try {
                userDB.delete(db.getDBName());
            } catch (Exception e) {
                // Do nothing.
            }

        }
		
		// 1) Login: username + password
		// 2) initConnection
		// 3) open GUI ==> in login!!!
	}

	// Needs to become private after testing
	public final static boolean initConnection(String dbName, String user,
            String password) {
		// TODO test login data + dbname
        try {
            //This procedure handles the connection initialization.
            Class.forName("org.sqlite.JDBC");
            connection = DriverManager.getConnection("jdbc:sqlite:" + dbName, 
            		user, password);
            // TODO '.db3' weglaten??
        } catch (Exception e) {
            // TODO bezien
        	e.printStackTrace(); // remove
            return false;
        }
        return true;
	}
	
	private final static void initUserDB() {
		
		if (!userDBExists()) {
            if (!userDB.createUserDBFile()) {
            	// TODO popup window -> onverwachte fout!
            }
            try {
                initUserDBConnection(userDBName, username, password);
                userDB.init();
            } catch (Exception e) {
                // TODO
                e.printStackTrace();
            }
        } else {
        	try {
                initUserDBConnection(userDBName, username, password);
            } catch (Exception e) {
                // TODO
                e.printStackTrace();
            }
        }
	}
	
    private final static void initUserDBConnection(String dbName, String user,
            String password) throws Exception {

		//This procedure handles the connection initialization.
		Class.forName("org.sqlite.JDBC");

		userDBConnection = DriverManager.getConnection("jdbc:sqlite:" + dbName, 
				user, password);
	}
	
	private final static boolean userDBExists() {
		
		File f = new File(userDBName);
		if (f.exists()) {
			return true;
		}
		return false;
	}
	
	
	private final static String username = "USIMPros1.0";
	private final static String password = "ProjDatBas1.0";

}
